package com.hpxx.ylzswl.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.hpxx.ylzswl.R;
import com.hpxx.ylzswl.activity.AmendLoginPwdActivity;
import com.hpxx.ylzswl.activity.FeedBackActivity;
import com.hpxx.ylzswl.activity.MessageActivity;
import com.hpxx.ylzswl.activity.MyProfileActivity;
import com.hpxx.ylzswl.activity.SetActivity;
import com.hpxx.ylzswl.activity.UpDataPhoneActivity;
import com.hpxx.ylzswl.base.BaseEvent;
import com.hpxx.ylzswl.base.BaseFragment;
import com.hpxx.ylzswl.bean.UserBean;
import com.hpxx.ylzswl.dialog.BaseDialog;
import com.hpxx.ylzswl.dialog.QuitLoginDialog;
import com.hpxx.ylzswl.event.NameEvent;
import com.hpxx.ylzswl.event.NoticeEvent;
import com.hpxx.ylzswl.event.PersonalEvent;
import com.hpxx.ylzswl.event.PhoneEvent;
import com.hpxx.ylzswl.event.QuitLoginEvent;
import com.hpxx.ylzswl.https.HttpAddress;
import com.hpxx.ylzswl.https.RequestParams;
import com.hpxx.ylzswl.utils.ConstantsUtils;
import com.hpxx.ylzswl.views.RoundImageView;
import com.universal.frame.generalutils.JsonUtil;
import com.universal.frame.generalutils.NetWorkUtil;
import com.universal.frame.generalutils.SharedPreferencesUtil;
import com.universal.frame.generalutils.ToastUtil;
import com.universal.frame.okhttp.OKHttpUtils;
import com.universal.frame.okhttp.ResultCallBack;

import java.io.IOException;

import de.greenrobot.event.EventBus;
import de.greenrobot.event.Subscribe;
import okhttp3.Request;

/**
 * Created by Administrator on 2017/11/3.
 */
public class MyFragment extends BaseFragment implements View.OnClickListener,ResultCallBack{
    private int state;
    private RoundImageView iv_user_img;
    private LinearLayout ll_my_zl,ll_my_pw,ll_my_phone
            ,ll_my_feed,ll_my_message,ll_my_set;
    private QuitLoginDialog mQuitLoginDialog;
    private TextView tv_name;
    private TextView tv_quit_login;
    private TextView tv_phone;
    private ImageView iv_red;
    public static MyFragment newInstance(int state){
        MyFragment fragment = new MyFragment();
        Bundle bundle = new Bundle();
        bundle.putInt(ConstantsUtils.FRAGMENT_FLG, state);
        fragment.setArguments(bundle);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.state = getArguments().getInt(ConstantsUtils.FRAGMENT_FLG);
    }
    private void updateNoticeReadPost(){
        OKHttpUtils.postAsync(getContext()
                , HttpAddress.NOTICE_RED_URL
                , new RequestParams(getContext()).getNoticeRedParams(3), this
                ,false,1);
    }
    private void updatePersonlPost(){
        OKHttpUtils.postAsync(getContext()
                , HttpAddress.PERSONAL_URL
                , new RequestParams(getContext()).getPersonalParams(), this
                ,false,2);
    }
    @Override
    public int getLayoutResId() {
        return R.layout.fragment_my;
    }

    @Override
    public void initView(View view) {
//        String url = "http://img5.imgtn.bdimg.com/it/u=3133775958,909341501&fm=27&gp=0.jpg";
//        iv_user_img = view.findViewById(R.id.iv_user_img);
//        GeneralUtil.loadImg(getActivity(),url,iv_user_img);
        iv_red = view.findViewById(R.id.iv_red);
        tv_phone= view.findViewById(R.id.tv_phone);
        tv_name = view.findViewById(R.id.tv_name);
        ll_my_zl = view.findViewById(R.id.ll_my_zl);
        ll_my_pw = view.findViewById(R.id.ll_my_pw);
        ll_my_phone = view.findViewById(R.id.ll_my_phone);
        ll_my_feed = view.findViewById(R.id.ll_my_feed);
        ll_my_message = view.findViewById(R.id.ll_my_message);
        ll_my_set = view.findViewById(R.id.ll_my_set);
        tv_quit_login = view.findViewById(R.id.tv_quit_login);
        ll_my_zl.setOnClickListener(this);
        ll_my_pw.setOnClickListener(this);
        ll_my_phone.setOnClickListener(this);
        ll_my_feed.setOnClickListener(this);
        ll_my_message.setOnClickListener(this);
        ll_my_set.setOnClickListener(this);
        tv_quit_login.setOnClickListener(this);
        tv_name.setText(SharedPreferencesUtil.getString(getContext(), ConstantsUtils.USER_NAME));
        tv_phone.setText(SharedPreferencesUtil.getString(getContext(), ConstantsUtils.USER_PHONE));
        updateNoticeReadPost();
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.ll_my_zl: //我的资料
                startActivity(new Intent(getActivity(), MyProfileActivity.class));
                break;

            case R.id.ll_my_pw: //修改密码
                if (NetWorkUtil.isNetWorkConnected(getContext())) {
                    startActivity(new Intent(getActivity(), AmendLoginPwdActivity.class));
                }else {
                    ToastUtil.showToast(getContext(), R.string.toast_network_connection_tips);
                }

                break;

            case R.id.ll_my_phone: //修改手机
                if (NetWorkUtil.isNetWorkConnected(getContext())) {
                    startActivity(new Intent(getActivity(), UpDataPhoneActivity.class));
                }else {
                    ToastUtil.showToast(getContext(), R.string.toast_network_connection_tips);
                }

                break;

            case R.id.ll_my_feed: //意见反馈
                if (NetWorkUtil.isNetWorkConnected(getContext())) {
                    startActivity(new Intent(getActivity(), FeedBackActivity.class));
                }else {
                    ToastUtil.showToast(getContext(), R.string.toast_network_connection_tips);
                }

                break;

            case R.id.ll_my_message: //消息中心
                if (NetWorkUtil.isNetWorkConnected(getContext())) {
                    startActivity(new Intent(getActivity(), MessageActivity.class));
                }else {
                    ToastUtil.showToast(getContext(), R.string.toast_network_connection_tips);
                }

                break;

            case R.id.ll_my_set: //设置
                startActivity(new Intent(getActivity(), SetActivity.class));
                break;

            case R.id.tv_quit_login: //退出登录
                quitLoginDialog();
                break;

            default:
                break;

        }
    }

    private void quitLoginDialog(){
        mQuitLoginDialog  = new QuitLoginDialog(getActivity(),"确定要切换登录吗", R.style.MyDialogStyle, new BaseDialog.LeaveMyDialogListener() {

            @Override
            public void onClick(View view) {
                switch (view.getId()) {
                    case R.id.bt_quit_qx:
                        mQuitLoginDialog.dismiss();
                        break;
                    case R.id.bt_quit_ok:
                        mQuitLoginDialog.dismiss();
                        //用户Id
                        SharedPreferencesUtil.putString(getContext(), ConstantsUtils.USER_ID, "");
                        //用户姓名
                        SharedPreferencesUtil.putString(getContext(), ConstantsUtils.USER_NAME, "");
                        //用户密码
                        SharedPreferencesUtil.putString(getContext(), ConstantsUtils.USER_PSW, "");
                        //用户性别
                        SharedPreferencesUtil.putString(getContext(), ConstantsUtils.USER_SEX, "");
                        //用户生日
                        SharedPreferencesUtil.putString(getContext(), ConstantsUtils.USER_BTD,"");
                        //服务电话
                        SharedPreferencesUtil.putString(getContext(), ConstantsUtils.USER_SERVERTEL,"");
                        SharedPreferencesUtil.putBoolean(getContext(), ConstantsUtils.IS_LOGIN, false);
                        EventBus.getDefault().post(new QuitLoginEvent());
                        getActivity().finish();
                        break;

                    default:
                        break;
                }
            }
        });
        mQuitLoginDialog.setCanceledOnTouchOutside(false);
        mQuitLoginDialog.show();
    }
    @Override
    @Subscribe
    public void onEventMainThread(BaseEvent event) throws IOException {
        super.onEventMainThread(event);
        if (event instanceof NameEvent) {

            tv_name.setText(((NameEvent) event).getName());
        }
        if (event instanceof PhoneEvent) {

            tv_phone.setText(((PhoneEvent) event).getPhone());
        }
        if (event instanceof NoticeEvent) {
            updateNoticeReadPost();
        }
        if (event instanceof PersonalEvent) {
            updatePersonlPost();
        }
    }

    @Override
    public void onSuccess(String response, int flag) {
        String status = JsonUtil.getString(response, "status", "");
        if (flag ==1){
            if("1".equals(status)){
                String data =  JsonUtil.getString(response, "data", "");
                String noticeRead = JsonUtil.getString(data, "noticeRead", "");
                if ("0".equals(noticeRead)){
                    iv_red.setVisibility(View.VISIBLE);
                }else {
                    iv_red.setVisibility(View.INVISIBLE);
                }
            }else{
                String msg = JsonUtil.getString(response, "msg", "");
                ToastUtil.showToast(getActivity(), msg);
            }
        }
        if (flag == 2){
            if("1".equals(status)){
                String data =  JsonUtil.getString(response, "data", "");
                String logistic = JsonUtil.getString(data, "logistic", "");
                UserBean userBean =  JSON.parseObject(logistic,UserBean.class);
                SharedPreferencesUtil.putString(getContext(), ConstantsUtils.USER_NAME,userBean.logisticRealname);

                tv_name.setText(SharedPreferencesUtil.getString(getContext(), ConstantsUtils.USER_NAME));

            }else{
                String msg = JsonUtil.getString(response, "msg", "");
                ToastUtil.showToast(getActivity(), msg);
            }
        }

    }

    @Override
    public void onFailure(Request request, IOException e, int flag) {

    }
}
